﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.IO;
using System.Xml;
using System.Reflection;

namespace Fishes.Objects
{
    public class Tool
    {
        #region Properties

        public string Name
        {
            get { return _name; }
        }
        private string _name = String.Empty;

        public Vector2 Size
        {
            get { return _size; }
        }
        private Vector2 _size = new Vector2();

        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }
        private Vector2 _position = Vector2.Zero;

        public Texture2D BackgroundImage
        {
            get { return _backgroundImage; }
        }
        private Texture2D _backgroundImage = null;

        public bool IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; }
        }
        private bool _isSelected = false;

        public IToolAction ToolAction
        {
            get { return _toolAction; }
        }
        private IToolAction _toolAction = null;

        #endregion

        #region Public Methods

        public bool Load(string path, GraphicsDevice g, Vector2 size)
        {
            try
            {
                FileStream fp = File.Open(path, FileMode.Open);

                XmlDocument doc = new XmlDocument();
                doc.Load(fp);

                this._name = doc.SelectSingleNode("tool/name").InnerText;

                if (this.BackgroundImage != null)
                {
                    this._backgroundImage.Dispose();
                    this._backgroundImage = null;
                }

                this._size = size;

                string backgroundFileName = doc.SelectSingleNode("tool/image").InnerText;
                this._backgroundImage = Texture2D.FromFile(g, backgroundFileName,
                    (int)this.Size.X, (int)this.Size.Y);

                string action = doc.SelectSingleNode("tool/action").InnerText;

                Assembly asm = Assembly.GetCallingAssembly();
                Type type = asm.GetType(action);
                if (type == null)
                    throw new NullReferenceException(String.Format("Failed to find the action class: {0}",
                        action));

                ConstructorInfo con = type.GetConstructor(new Type[] { });

                IToolAction toolAction = con.Invoke(new object[] { }) as IToolAction;
                if (toolAction == null)
                    throw new NullReferenceException("'toolAction' is null.");

                this._toolAction = toolAction;

                fp.Close();
                fp.Dispose();
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        public bool Destroy()
        {
            this.BackgroundImage.Dispose();
            this._backgroundImage = null;

            return true;
        }

        public void Render(SpriteBatch sb)
        {
            Rectangle dest = new Rectangle();

            dest.X = (int)this.Position.X;
            dest.Y = (int)this.Position.Y;
            dest.Width = (int)this.Size.X;
            dest.Height = (int)this.Size.Y;

            if(this.IsSelected)
                sb.Draw(this.BackgroundImage, dest, Color.Red);
            else
                sb.Draw(this.BackgroundImage, dest, Color.White);
        }

        public void OnMouseClick(Vector2 pos)
        {
        }

        public void OnMouseUp(Game1 engine)
        {
            if (this.ToolAction == null)
                return;

            this.ToolAction.Action(engine);
        }

        #endregion
    }
}
